﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using BLL;
using Model;
using WebApplication1.App_Code;
using System.Linq;

namespace WebApplication1.RecordManagement
{
    public partial class ToolRecordAdd : System.Web.UI.Page
    {
        private enum_Record_Mode Record_Mode
        {
            get { return (enum_Record_Mode)Enum.Parse(typeof(enum_Record_Mode), ViewState["Record_Mode"].ToString()); }
            set { ViewState["Record_Mode"] = value; }
        }
        private ToolRecord tr;
        public Config c;
        private string message;
        private Tool_Code tc;

        public ToolRecordAdd()
        {
            tr = new ToolRecord();
            c = new Config();
            tc = new Tool_Code();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindRecordMode();
                BindDropDownList(this.DDL_WorkShop, enum_configType.WORK_SHOP);
                BindDropDownList(this.DDL_ToolType, enum_configType.TOOL_TYPE);
                BindDropDownList(this.DDL_DistributionArea, enum_configType.DISTRIBUTION_AREA);
                BindDropDownList(this.DDL_ToolState, enum_configType.TOOL_STATE);
                BindDropDownList(this.DDL_RepairRecord, enum_configType.REPAIR_RECORD);

                Record_Mode = enum_Record_Mode.入库;
                this.rfv_Taker.Enabled = false;
                this.rfv_Taker_ID.Enabled = false;
                this.DDL_DistributionArea.SelectedValue = "工具房";
                this.DDL_DistributionArea.Enabled = false;
                this.SpanTT.Visible = false;
                this.SpanTId.Visible = false;
                this.SpanTPY.Visible = false;
                this.SpanTPYN.Visible = false;
                
            }
            this.Lbl_EnableSN.Enabled =false;
            this.Btn_EnableSN_Add.Visible = true;
        }

        private void BindRecordMode()
        {
            IList<ListItem> list=new List<ListItem>();
            foreach (string str in Enum.GetNames(typeof(enum_Record_Mode)))
            {
                list.Add(new ListItem(str));
            }
            this.DDL_RecordMode.DataSource = list;
            this.DDL_RecordMode.DataBind();
        }

        public void BindDropDownList(DropDownList ddl, enum_configType ct)
        {
            ddl.DataSource = c.Config_GetAll_forSingleType(ct);
            ddl.DataTextField = "ItemName";
            ddl.DataValueField = "ItemName";
            ddl.DataBind(); 
        }

        protected void DDL_RecordMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            Record_Mode = (enum_Record_Mode)Enum.Parse(typeof(enum_Record_Mode), this.DDL_RecordMode.SelectedValue);
            switch (Record_Mode)
            {
                case enum_Record_Mode.入库:
                    this.rfv_Taker.Enabled = false;
                    this.rfv_Taker_ID.Enabled = false;
                    if (!DDL_DistributionArea.Items.Contains(new ListItem("工具房")))
                    {
                        this.DDL_DistributionArea.Items.Add("工具房");
                    }
                    this.DDL_DistributionArea.SelectedValue = "工具房";
                    this.DDL_DistributionArea.Enabled = false;
                    this.SpanTT.Visible = false;
                    this.SpanTId.Visible = false;
                    this.SpanTPY.Visible = false;
                    this.SpanTPYN.Visible = false;
                    break;
                case enum_Record_Mode.出库:
                    this.rfv_Taker.Enabled = true;
                    this.rfv_Taker_ID.Enabled = true;
                    this.DDL_DistributionArea.Items.Remove("工具房");
                    this.DDL_DistributionArea.Enabled = true;
                    this.SpanTT.Visible = true;
                    this.SpanTId.Visible = true;
                    break;
                case enum_Record_Mode.报废:
                    this.rfv_Taker.Enabled = false;
                    this.rfv_Taker_ID.Enabled = false;
                    this.DDL_DistributionArea.Items.Remove("工具房");
                    this.DDL_DistributionArea.Enabled = true;
                    this.SpanTT.Visible = false;
                    this.SpanTId.Visible = false;
                    break;
            }
        }

        protected void RBL_ProductType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindProductType();
        }

        private void BindProductType()
        {
            if (RBL_ProductType.SelectedValue == "通用")
            {
                this.Tb_ProductType.Text = "通用";
                this.Tb_ProductType.Enabled = false;
                this.Tb_ProductTypeName.Text = "通用";
                this.Tb_ProductTypeName.Enabled = false;
                this.SpanTPY.Visible = false;
                this.SpanTPYN.Visible = false;
            }
            else
            {
                this.Tb_ProductType.Text = "";
                this.Tb_ProductType.Enabled = true;
                this.Tb_ProductTypeName.Text = "";
                this.Tb_ProductTypeName.Enabled = true;
                this.SpanTPY.Visible = true;
                this.SpanTPYN.Visible = true;
            }
        }

        protected void Tb_ToolCode_TextChanged(object sender, EventArgs e)
        {
           
            string ToolCode = this.Tb_ToolCode.Text.Trim(); 
            string WorkShop = this.DDL_WorkShop.SelectedValue;
            //Tool_CodeInfo TcInfo = tc.GetbyTcodeExis(ToolCode, WorkShop);
            //if (!string.IsNullOrEmpty(TcInfo.Tool_Code) && !string.IsNullOrEmpty(TcInfo.Work_Shop))
            //{
            //    string str1 = "此代码在" + WorkShop + "已存在,请输入新代码！";
            //    ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
            //    this.Tb_ToolCode.Text = "";
            //    this.Tb_ToolCode.Focus();
            //}
            //else
            //{
                if (TCodeCheck(ToolCode, out message)==false)
                {
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
                    this.Tb_ToolCode.Text = "";
                    this.Tb_ToolCode.Focus();
                }
                else
                {
                       // 已入库的数据  在出库 和报废的时候 同一个代码的类别 和名称要一致,再次入库也要和之前的一致

                    if (this.DDL_RecordMode.SelectedValue != "入库")
                    {
                        ToolRecordInfo trInfo = tr.GetbyTypeandName(ToolCode, WorkShop);
                        this.DDL_ToolType.SelectedValue = trInfo.Tool_Type;
                        this.Tb_ToolName.Text = trInfo.Tool_Code;
                        this.DDL_WorkShop.SelectedValue = trInfo.Work_Shop;
                        this.Tb_ToolTypeSpec.Text = trInfo.Tool_Type_Spec;
                        if (trInfo.Tool_Type == null
                            && trInfo.Tool_Code == null
                            && trInfo.Work_Shop == null)
                        {
                            

                            string strs = "不存在入库,不能出库！";
                            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + strs + "');</script>");
                            this.Tb_ToolCode.Text = "";
                            this.Tb_ToolTypeSpec.Text = "";
                            this.Tb_ToolCode.Focus();
                            return;
                        }
                    }
                    else
                    {
                        tc = new Tool_Code();
                        Tool_CodeInfo tci = new Tool_CodeInfo { Tool_Code = this.Tb_ToolCode.Text.Trim() };
                        this.Tb_ToolName.Text = tc.Tool_Code_GetbyMultiFact(tci).Count == 0 ? "" : tc.Tool_Code_GetbyMultiFact(tci)[0].Tool_Type_Spec;
                        this.Tb_ToolTypeSpec.Text = tc.Tool_Code_GetbyMultiFact(tci).Count == 0 ? "" : tc.Tool_Code_GetbyMultiFact(tci)[0].Tool_Type_Spec;
                    }
                     

                }
            //}

        }

        #region 代码格式验证
        /// <summary>
        /// 代码格式验证
        /// </summary>
        /// <param name="TcodeSN"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool TCodeCheck(string TcodeSN, out string message)
        {
            if (CheckTCodeSnLength(TcodeSN, out message))
            {
                if (CheckBlankTcode(TcodeSN, out message))
                {
                    message = "";
                    return true;
                }

            }
            return false;
        }

        private static bool CheckTCodeSnLength(string TcodeSN, out string message)
        {
            if (TcodeSN.Length ==14)
            {
                message = "";
                return true;
            }
            message = "代码长度有误";
            return false;
        }
        /// <summary>
        /// 代码规则
        /// </summary>
        /// <param name="TcodeSn"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        private static bool CheckBlankTcode(string TcodeSn, out string message)
        {
            string SNArray1 = TcodeSn.Substring(1,1);
            string SNArray2 = TcodeSn.Substring(4, 1);
            string SNArray3 = TcodeSn.Substring(7, 1);
            string SNArray4 = TcodeSn.Substring(10,1);
            if (SNArray1 == "-" && SNArray2 == "-" && SNArray3 == "-" && SNArray4 == "-")
            {
                message = "";
                return true;
            }
            message = "代码格式有问题,正确格式如:X-XX-XX-XX-XXX";
            return false;
        } 
        #endregion

        protected void Btn_EnableSN_Add_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.Tb_Amount.Text))
            {
                this.Tb_Amount.Focus();
                return;
            }
            string tEsn = this.Tb_EnableSN.Text.Trim();
            int Amount = int.Parse(this.Tb_Amount.Text.Trim().ToString());
            string str1 = "";
            ToolRecordInfo TRInfo = tr.GetbytEsnExis(tEsn);
            if (!string.IsNullOrEmpty(tEsn))
            {  
                if (!string.IsNullOrEmpty(tEsn) && this.DDL_RecordMode.SelectedValue != "入库")
                {
                    if (TRInfo.Record_Mode != "报废")
                    {
                        if (string.IsNullOrEmpty(TRInfo.Tool_EnableSN) && TRInfo.Work_Shop!=this.DDL_WorkShop.SelectedValue)
                        {
                            str1 = "此启用编码不存在入库！";
                            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
                            //this.Tb_EnableSN.Text = "";
                            this.Tb_EnableSN.Focus();
                            return;
                        }
                        else
                        {
                            if (Amount > TRInfo.Amount && this.DDL_RecordMode.SelectedValue == TRInfo.Record_Mode)
                            {
                                str1 = "此启用编码库存数量不足！";
                                ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
                                //this.Tb_Amount.Text = "";
                                this.Tb_Amount.Focus();
                                return;
                            }
                            else if (this.DDL_RecordMode.SelectedValue == "报废" && TRInfo.Record_Mode != "出库")
                            {
                                str1 = "此启用编码不存在出库！";
                                ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
                                //this.Tb_EnableSN.Text = "";
                                this.Tb_EnableSN.Focus();
                                return;
                            }

                        }
                    }
                    else
                    {
                        str1 = "此启用编码已报废！";
                        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
                        this.Tb_EnableSN.Focus();
                        return;
                    }
                   
                }
                if (!string.IsNullOrEmpty(TRInfo.Tool_EnableSN) && this.DDL_RecordMode.SelectedValue == "入库")
                {
                    str1 = "此启用编号已存在,请输入新启用编号！";
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
                    this.Tb_EnableSN.Text = "";
                    this.Tb_EnableSN.Focus();
                    return;
                }
                else
                {
                    this.ButEditor.Visible = true;
                    this.Lbl_EnableSN.Visible = true;
                    this.Span1.Visible = true;
                    #region 多个启用编码规则
                    string str = "";
                    string t = "";
                    string kj = "";
                    //string dh = "";
                    if (this.Lb_EnableSN.Items.Count == 0)
                    {
                        this.Lb_EnableSN.Items.Add(new ListItem(this.Tb_EnableSN.Text.Trim(), this.Tb_Amount.Text.Trim()));

                        foreach (ListItem li in this.Lb_EnableSN.Items)
                        {
                            t = li.Text.Trim();

                            int i = int.Parse(t.Substring(t.Length - 2, 2));
                            i++;
                            string s = "";
                            string r = t.Substring(t.Length - 2, 2);
                            if (r.Substring(0, 1) == "0" && i != 10)
                            {
                                s = t.Substring(0, t.Length - 1);
                            }
                            else
                            {
                                s = t.Substring(0, t.Length - 2);
                            }
                            string st = s + i;

                            str = str + st + ",";  //":" + li.Value + 

                        }
                        this.Lbl_EnableSN.Text = str.TrimEnd(',');
                    }
                    else if (this.Lb_EnableSN.Items.Count == 1)
                    {
                        this.Lb_EnableSN.Items.Add(new ListItem(this.Lbl_EnableSN.Text, this.Tb_Amount.Text.Trim()));
                        foreach (ListItem li in this.Lb_EnableSN.Items)
                        {
                            string eSN = li.Text.Trim();
                            string[] sArray = eSN.Split(',');
                            t = sArray[0];
                            int j = int.Parse(t.Substring(t.Length - 2, 2));
                            j++;

                            string k = "";
                            string r = t.Substring(t.Length - 2, 2);
                            if (r.Substring(0, 1) == "0" && j != 10)
                            {
                                k = t.Substring(0, t.Length - 1);
                            }
                            else
                            {
                                k = t.Substring(0, t.Length - 2);
                            }


                            kj = k + j;

                            str = str + kj + ","; //":" + li.Value + 
                        }
                        this.Lbl_EnableSN.Text = str.TrimEnd(',');
                    }
                    else
                    {
                        string strSN = "";
                        this.Lb_EnableSN.Items.Add(new ListItem(this.Lbl_EnableSN.Text, this.Tb_Amount.Text.Trim()));
                        if (this.Lb_EnableSN.Items.Count <= 10)
                        {

                            foreach (ListItem li in this.Lb_EnableSN.Items)
                            {
                                string eSN = li.Text.Trim();
                                if (eSN == this.Lbl_EnableSN.Text)
                                {
                                    string[] sArray = eSN.Split(','); // eSN.Split(':');new char[2] { ',', ':' }

                                    t = sArray[sArray.Length - 1];


                                    int j = int.Parse(t.Substring(t.Length - 2, 2));
                                    j++;
                                    string k = "";
                                    string rs = t.Substring(t.Length - 2, 2);
                                    if (rs.Substring(0, 1) == "0" && j != 10)
                                    {
                                        k = t.Substring(0, t.Length - 1);
                                    }
                                    else
                                    {
                                        k = t.Substring(0, t.Length - 2);
                                    }

                                    kj = k + j;

                                    strSN = kj + ",";//":" + li.Value + 
                                }
                                str = eSN + "," + strSN;
                            }
                            this.Lbl_EnableSN.Text = str.TrimEnd(',');
                        }

                    }
                    #endregion

                }
            }
            else
            {
                str1 = "请输入用编号！";
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
                this.Tb_EnableSN.Text = "";
                this.Tb_EnableSN.Focus();
                return;
            }
        }

        /// <summary>
        /// 数据录入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Btn_ToolRecord_Add_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.Tb_Amount.Text))
            {
                this.Tb_Amount.Focus();
                return; 
            }
            Record_Mode = (enum_Record_Mode)Enum.Parse(typeof(enum_Record_Mode), this.DDL_RecordMode.SelectedValue);
            string txEsn = this.Tb_EnableSN.Text.Trim();
            IList<ToolRecordInfo> list = new List<ToolRecordInfo>();
            ToolRecordInfo TRInfo = tr.GetbytEsnExis(txEsn);
            if (!string.IsNullOrEmpty(TRInfo.Tool_EnableSN) && this.DDL_RecordMode.SelectedValue== "入库")
            { 
                string str1 = "此启用编号已存在,请输入新启用编号！";
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
                this.Tb_EnableSN.Text = "";
                this.Tb_EnableSN.Focus();
            }
            else
            {
                switch (Record_Mode)
                {
                    case enum_Record_Mode.入库:
                        Tstates("入库", TRInfo);
                        break;
                    case enum_Record_Mode.出库:
                        Tstates("出库", TRInfo);
                        break;
                    case enum_Record_Mode.报废:
                        Tstates("报废", TRInfo);
                        break;
                    default:
                        break;
                }

            }
        }

        private void Tstates(string states, ToolRecordInfo TRInfo) 
        {
            
            string tEsn = this.Tb_EnableSN.Text.Trim();
            string tCode = this.Tb_ToolCode.Text.Trim();
            string workShop=this.DDL_WorkShop.SelectedValue;
            string distribution_Area = this.DDL_DistributionArea.SelectedItem.Text;
            string decordMode = this.DDL_RecordMode.SelectedItem.Text;
            int Amount = int.Parse(this.Tb_Amount.Text.Trim().ToString());
            int inventoryAmount = 0;//库存数
            ToolRecordInfo TRdInfo = tr.GetbytEsnExisTcode(tEsn, tCode);//GetbytEsnExis(tEsn);
            ToolRecordInfo tri = new ToolRecordInfo
            {
                ID = 0,
                Record_Mode = "",
                Record_Date = DateTime.Now,
                Work_Shop = null,
                Tool_Type = null,
                ProductType = null,
                ProductTypeName = null,
                Tool_Code = tCode,
                Tool_Name = null,
                Tool_Type_Spec = "",
                Tool_EnableSN = null,
                Amount = 0,
                Taker = null,
                Taker_ID = null,
                Distribution_Area = null,
                Tool_State = null,
                Repair_Record = null,
                Remark = ""
            };
            IList<ToolRecordInfo> list = tr.ToolRecord_StatisticsbyMultiFact(tri);
            foreach (ToolRecordInfo trinfo in list)
            {
                if (trinfo.Distribution_Area == "工具房" )
                {
                    inventoryAmount = trinfo.Amount;
                    break;
                } 
            }
            string str1 = "";
            if (states == "入库" || states == "出库")
            {
                if (!string.IsNullOrEmpty(tEsn) && states != "入库")//有启用编码出库
                {
                    if (string.IsNullOrEmpty(TRdInfo.Tool_Code) && string.IsNullOrEmpty(TRdInfo.Tool_EnableSN))
                    {
                        string strs = "不存在入库,不能出库！";
                        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + strs + "');</script>");
                        return;
                    }
                    if (TRdInfo.Record_Mode != "报废")//已报废的启用编码不能再次出库
                    {
                        if (string.IsNullOrEmpty(TRdInfo.Tool_EnableSN))
                        {
                            str1 = "此启用编码不存在入库！";
                            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
                            //this.Tb_EnableSN.Text = "";
                            this.Tb_EnableSN.Focus();
                            return;
                        }
                        else
                        {
                            //如果方式相同  并且 出库数量 小于或等于入库
                            if (this.DDL_RecordMode.SelectedValue == TRdInfo.Record_Mode && Amount > inventoryAmount)//TRdInfo.Amount
                            {
                                str1 = "此启用编码库存数量不足！";
                                ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
                                //this.Tb_Amount.Text = "";
                                this.Tb_Amount.Focus();
                                return;
                            }
                        }
                    }
                    else
                    {
                        str1 = "此启用编码已报废！";
                        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
                        this.Tb_EnableSN.Focus();
                        return;
                    }
                }
                else if (string.IsNullOrEmpty(tEsn) && states != "入库")//无启用编码出库
                {
                    //类型相同 Amount 入库数  出库数量 小于或等于入库  
                    if (this.Tb_ToolTypeSpec.Text == TRdInfo.Tool_Type_Spec && Amount > inventoryAmount )//TRdInfo.Amount
                    {
                        str1 = "库存数量不足！";
                        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
                        //this.Tb_Amount.Text = "";
                        this.Tb_Amount.Focus();
                        return;
                    }
                    
                }
              TRInfoAdd();
                    
            } 
            else//如果是报废
            {
                TRInfo = tr.GetbyTypeandName(tCode, workShop);
                //是否在同一区域 

                if (TRInfo.Distribution_Area == this.DDL_DistributionArea.SelectedValue && TRInfo.Work_Shop == this.DDL_WorkShop.SelectedValue)//
                {
                    if (!string.IsNullOrEmpty(tEsn))
                    {
                        if (TRdInfo.Record_Mode != "报废")
                        {
                            if (string.IsNullOrEmpty(TRdInfo.Tool_EnableSN) && TRInfo.Record_Mode == "入库")
                            {
                                str1 = "此启用编码不存在出库！";
                                ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
                                //this.Tb_EnableSN.Text = "";
                                this.Tb_EnableSN.Focus();
                                return;
                            }
                            else
                            {
                                if (Amount >inventoryAmount  && TRInfo.Record_Mode == "报废")//TRdInfo.Amount
                                {
                                    str1 = "此启用编码库存数量不足！";
                                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
                                    //this.Tb_Amount.Text = "";
                                    this.Tb_Amount.Focus();
                                    return;
                                }
                            }
                        }
                        else
                        {
                            str1 = "此启用编码已报废！";
                            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
                            this.Tb_EnableSN.Focus();
                            return;
                        }
                    }
                    else   //无启用编码报废
                    {
               
                        //类型相同 Amount 报废数  报废数量 小于或等于入库  
                        if (this.Tb_ToolTypeSpec.Text == TRdInfo.Tool_Type_Spec && Amount >inventoryAmount )//TRdInfo.Amount
                        {
                            str1 = "库存数量不足！";
                            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
                            //this.Tb_Amount.Text = "";
                            this.Tb_Amount.Focus();
                            return;
                        }
                    
                    }
                   
                    TRInfoAdd(); 
                }
                //不是,提示本启用编号不是在本区域入库的 不能报废
                else
                {                    
                    str1 = "不在同车间.同区域入库,或无出库,不能报废！";
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
                    this.Tb_EnableSN.Focus();
                } 
            } 
        
        }
        /// <summary>
        /// 数据添加
        /// </summary>
        public void TRInfoAdd()
        {
            IList<ToolRecordInfo> list = new List<ToolRecordInfo>();
            if (!string.IsNullOrEmpty(Lbl_EnableSN.Text))
            {
                //this.Lb_EnableSN.Items.Add(new ListItem(this.Lbl_EnableSN.Text.Trim(), this.Tb_Amount.Text.Trim()));
                string tl = this.Tb_EnableSN.Text.Trim();
                string srt = this.Lbl_EnableSN.Text.Trim();
                string strs = tl + "," + srt;
                string[] sArray = strs.Split(',');

                this.Lb_EnableSN.Items.Clear();
                for (int i = 0; i < sArray.Length; i++)
                {
                    this.Lb_EnableSN.Items.Add(new ListItem(sArray[i], this.Tb_Amount.Text.Trim()));
                }
            }
            else
            {
                this.Lb_EnableSN.Items.Add(new ListItem(this.Tb_EnableSN.Text.Trim(), this.Tb_Amount.Text.Trim()));
            }

            foreach (ListItem li in this.Lb_EnableSN.Items)
            {
                ToolRecordInfo tri = new ToolRecordInfo
                {
                    Record_Mode = this.Record_Mode.ToString(),
                    Record_Date = DateTime.Parse(this.Tb_RecordDate.Text + " 00:00:00"),
                    Work_Shop = this.DDL_WorkShop.SelectedValue,
                    Tool_Type = this.DDL_ToolType.SelectedValue,
                    ProductType = this.Tb_ProductType.Text.Trim(),
                    ProductTypeName = this.Tb_ProductTypeName.Text.Trim(),
                    Tool_Name = this.Tb_ToolName.Text.Trim(),
                    Tool_Code = this.Tb_ToolCode.Text.Trim(),
                    Tool_Type_Spec = this.Tb_ToolTypeSpec.Text.Trim(),
                    Tool_EnableSN = li.Text,
                    Amount = int.Parse(li.Value),
                    Taker = this.Tb_Taker.Text.Trim(),
                    Taker_ID = this.Tb_Taker_ID.Text.Trim(),
                    Distribution_Area = this.DDL_DistributionArea.SelectedValue,
                    Tool_State = this.DDL_ToolState.SelectedValue,
                    Repair_Record = this.DDL_RepairRecord.SelectedValue,
                    Remark = this.Tb_Remark.Text.Trim()
                };
                list.Add(tri);
            }
            foreach (ToolRecordInfo tri in list)
            {
                //string a = this.Tb_Amount.Text.Trim();
                string r = tri.Tool_EnableSN;//tri.Tool_EnableSN;
                string[] sArray = r.Split(','); // eSN.Split(':');new char[2] { ',', ':' }
                for (int i = 0; i < sArray.Length; i++)
                {
                    //if (sArray[i] != a)
                    //{
                        tri.Tool_EnableSN = sArray[i];
                    //}
                }
                tr.Insert(tri);
            }
            string str = "提交成功！";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str + "');</script>");
            //Clear_Control();
            Response.Redirect("~/RecordManagement/ToolRecordManagement.aspx"); 

        }



        private void Clear_Control()
        {
            this.Lb_EnableSN.Items.Clear();
            this.Lbl_EnableSN.Text = "";

            this.Tb_RecordDate.Text = "";
            this.RBL_ProductType.SelectedValue = "通用";
            BindProductType();
            this.Tb_ToolName.Text = "";
            this.Tb_ToolCode.Text = "";
            this.Tb_ToolTypeSpec.Text = "";
            this.Tb_EnableSN.Text = "";
            this.Tb_Amount.Text = "";
            this.Tb_Taker.Text = "";
            this.Tb_Taker_ID.Text = "";
            this.Tb_Remark.Text = "";
        }

        /// <summary>
        /// 编辑启用编号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButEditor_Click(object sender, EventArgs e)
        {
          //string tl = this.Tb_EnableSN.Text.Trim();
          //string srt= this.Lbl_EnableSN.Text.Trim();
          //string str = tl+","+ srt;
          //string[] sArray = str.Split(',');

          //this.Lb_EnableSN.Items.Clear();
          //for (int i = 0; i < sArray.Length; i++)
          //{
          //    this.Lb_EnableSN.Items.Add(new ListItem(sArray[i], this.Tb_Amount.Text.Trim()));
          //}
            this.Lbl_EnableSN.Enabled =true;
        }


        /// <summary>
        /// 判断 启用编号是否为空 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Tb_EnableSN_TextChanged(object sender, EventArgs e)
        { 
            if (string.IsNullOrEmpty(this.Tb_EnableSN.Text.Trim().ToString()))
            {
                this.Tb_Amount.Enabled = true;
                //this.Btn_EnableSN_Add.Visible = false;
                this.Lbl_EnableSN.Text = "";
                this.Lbl_EnableSN.Visible = false;
                this.Span1.Visible = false;
                this.ButEditor.Visible = false;
                this.Tb_Amount.Text = "";
            }
            else 
            {
                this.Tb_Amount.Enabled = false;
                this.Tb_Amount.Text = "1";  //如启用编号不为空 对应的数量都为1
                this.Btn_EnableSN_Add.Visible = true;
                this.Lbl_EnableSN.Visible = true;
                this.Span1.Visible = true;
                this.ButEditor.Visible = true;
            }

            //报废时分布区域、状态、修复记录、备注自动要能自动调取
            string Esn = this.Tb_EnableSN.Text.Trim();
            if (this.DDL_RecordMode.SelectedValue == "报废" && !string.IsNullOrEmpty(Esn)) 
            { 
                ToolRecordInfo TRInfo = tr.GetbytEsnExis(Esn);
                if (TRInfo != null&&TRInfo.Record_Mode=="出库")
                {
                    DDL_DistributionArea.SelectedValue =TRInfo.Distribution_Area;
                    DDL_ToolState.SelectedValue = TRInfo.Tool_State;
                    DDL_RepairRecord.SelectedValue = TRInfo.Repair_Record;
                    Tb_Remark.Text = TRInfo.Remark;
                }
                else 
                {
                    string str2 = "此启用编码不存在出库！";
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str2 + "');</script>");
                    this.Tb_EnableSN.Focus();
                    return;
                }
            
            }
        } 
    }
}
